// pages/orders/orders.js
const app = getApp()

Page({
  data: {
    currentTab: 'all',
    orders: [],
    loading: false
  },

  onLoad(options) {
    if (options.status) {
      this.setData({
        currentTab: options.status
      })
    }
    this.loadOrders()
  },

  // 切换标签
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab
    this.setData({
      currentTab: tab
    })
    this.loadOrders()
  },

  // 加载订单列表
  loadOrders() {
    this.setData({
      loading: true
    })

    wx.request({
      url: `${app.globalData.baseUrl}/mini/order/list`,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        status: this.data.currentTab === 'all' ? null : this.data.currentTab
      },
      success: (res) => {
        this.setData({
          loading: false
        })
        
        if (res.data.code === 1) {
          this.setData({
            orders: res.data.data || []
          })
        } else {
          wx.showToast({
            title: res.data.msg || '加载失败',
            icon: 'none'
          })
        }
      },
      fail: (err) => {
        this.setData({
          loading: false
        })
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        })
      }
    })
  },

  // 查看订单详情
  viewOrderDetail(e) {
    const orderId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/order-detail/order-detail?orderId=${orderId}`
    })
  },

  // 取消订单
  cancelOrder(e) {
    const orderId = e.currentTarget.dataset.id
    
    wx.showModal({
      title: '确认取消',
      content: '确定要取消这个订单吗？',
      success: (res) => {
        if (res.confirm) {
          this.performCancelOrder(orderId)
        }
      }
    })
  },

  // 执行取消订单
  performCancelOrder(orderId) {
    wx.showLoading({
      title: '取消中...'
    })

    wx.request({
      url: `${app.globalData.baseUrl}/mini/order/cancel`,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        orderId: orderId
      },
      success: (res) => {
        wx.hideLoading()
        
        if (res.data.code === 1) {
          wx.showToast({
            title: '订单已取消',
            icon: 'success'
          })
          this.loadOrders()
        } else {
          wx.showToast({
            title: res.data.msg || '取消失败',
            icon: 'none'
          })
        }
      },
      fail: (err) => {
        wx.hideLoading()
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        })
      }
    })
  },

  // 确认收货
  confirmReceive(e) {
    const orderId = e.currentTarget.dataset.id
    
    wx.showModal({
      title: '确认收货',
      content: '确认已收到商品吗？',
      success: (res) => {
        if (res.confirm) {
          this.performConfirmReceive(orderId)
        }
      }
    })
  },

  // 执行确认收货
  performConfirmReceive(orderId) {
    wx.showLoading({
      title: '确认中...'
    })

    wx.request({
      url: `${app.globalData.baseUrl}/mini/order/confirm`,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        orderId: orderId
      },
      success: (res) => {
        wx.hideLoading()
        
        if (res.data.code === 1) {
          wx.showToast({
            title: '确认收货成功',
            icon: 'success'
          })
          this.loadOrders()
        } else {
          wx.showToast({
            title: res.data.msg || '确认失败',
            icon: 'none'
          })
        }
      },
      fail: (err) => {
        wx.hideLoading()
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        })
      }
    })
  },

  // 再次购买
  buyAgain(e) {
    const orderId = e.currentTarget.dataset.id
    // 这里可以将订单商品重新加入购物车
    wx.showToast({
      title: '已加入购物车',
      icon: 'success'
    })
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.loadOrders()
    wx.stopPullDownRefresh()
  }
})